package com.jelly.web.modules.basics.bulletin.controller;

import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.note.OperationLog;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.basics.bulletin.domain.Bulletin;
import com.jelly.web.modules.basics.bulletin.domain.BulletinAndUser;
import com.jelly.web.modules.basics.bulletin.service.IBulletinAndUserService;
import com.jelly.web.modules.basics.bulletin.service.IBulletinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;

/**
 * @创建人 ql
 * @创建时间 2018-09-29 14:24
 * @项目名称 jelly
 * @功能描述: 公告控制器
 **/
@Controller
@RequestMapping("/admin/basics/bulletin")
public class BulletinController extends BaseController {

    @Autowired
    private IBulletinAndUserService bulletinAndUserService;

    @Autowired
    private IBulletinService bulletinService;

    /**
     * 公告列表跳转
     * @return
     */
    @GetMapping
    public String getBulletinList(){

        //获取字典详情
        getDictionary();
        //获取该用户可访问的按钮详情
        getButton();

        return adminBasicsBulletin("/list");

    }

    /**
     * 公告列表详情
     * @param bulletin
     * @return
     */
    @PostMapping
    @ResponseBody
    public LayuiTableInfo getBulletinList(BulletinAndUser bulletin){

        getPageHelper();

        return getPageHelper(bulletinAndUserService.getBulletinAndUser(bulletin));

    }


    /**
     * 新增跳转
     * @return
     */
    @GetMapping("add")
    public String addBulletin(){

        //获取字典详情
        getDictionary();

        return adminBasicsBulletin("/add");
    }

    /**
     * 新增公告
     * @param bulletin
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "公告模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addBulletin(Bulletin bulletin){

        if(StringUtils.isNotBlank(bulletin.getContent())){

            Set<String> set = verificationSensitiveWord(bulletin.getContent());

            if(set.size() > 0){

                return errorResponse("通告内容包含敏感词"+set.toString());

            }

        }

        return bulletinService.addBulletin(bulletin)?okResponse("新增成功"):errorResponse("新增失败");

    }

    /**
     * 删除公告
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "公告模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectBulletin(Base base){

        return bulletinService.delectBulletin(base.getBaseId())?okResponse("删除成功"):errorResponse("删除失败");

    }

    /**
     * 修改公告
     * @param model
     * @param base
     * @return
     */
    @GetMapping("/update")
    public String updateBulletin(Model model, Base base){

        //获取字典详情
        getDictionary();

        model.addAttribute("bulletin",bulletinAndUserService.getBulletinAndUser(base.getBaseId()));

        return adminBasicsBulletin("/update");

    }

    /**
     * 修改公告
     * @param bulletin
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "公告模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateBulletin(Bulletin bulletin){

        if(StringUtils.isNotBlank(bulletin.getContent())){

            Set<String> set = verificationSensitiveWord(bulletin.getContent());

            if(set.size() > 0){

                return errorResponse("通告内容包含敏感词"+set.toString());

            }

        }

        return bulletinService.updatBulletin(bulletin)?okResponse("修改成功"):errorResponse("修改失败");

    }

    /**
     * 公告详情页
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/info")
    public String infoBulletin(Model model, String id ){

        //获取字典详情
        getDictionary();

        model.addAttribute("bulletin",bulletinAndUserService.getBulletinAndUser(id));

        return adminBasicsBulletin("/bulletin");

    }

}
